<?php
/**
 * Groo base inclusion file.
 *
 * This file brings in all of the dependencies that every Groo
 * script will need and sets up objects that all scripts use.
 *
 * $Horde: groo/lib/base.php,v 1.46.10.4 2006/03/01 19:31:43 chuck Exp $
 *
 * Copyright 2001-2006 Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @since   Groo 1.0
 * @package Groo
 */

// Check for a prior definition of HORDE_BASE (perhaps by an
// auto_prepend_file definition for site customization).
if (!defined('HORDE_BASE')) {
    if (file_exists(dirname(__FILE__) . '/../../../lib/core.php')) {
        define('HORDE_BASE', dirname(__FILE__) . '/../../..');
    } else {
        define('HORDE_BASE', dirname(__FILE__) . '/../..');
    }
}

// Load the Horde Framework core, and set up inclusion paths.
require_once HORDE_BASE . '/lib/core.php';

// Registry.
$registry = &Registry::singleton();
if (is_a(($pushed = $registry->pushApp('groo', !defined('AUTH_HANDLER'))), 'PEAR_Error')) {
    if ($pushed->getCode() == 'permission_denied') {
        Horde::authenticationFailureRedirect();
    }
    Horde::fatal($pushed, __FILE__, __LINE__, false);
}
$conf = &$GLOBALS['conf'];
@define('GROO_TEMPLATES', $registry->get('templates'));

// Notification system.
$notification = &Notification::singleton();
$notification->attach('status');

// Find the base file path of Groo.
@define('GROO_BASE', dirname(__FILE__) . '/..');

// Groo libraries.
require_once GROO_BASE . '/lib/Groo.php';
require_once GROO_BASE . '/lib/Driver.php';

// Horde libraries.
require_once 'Horde/Text/Filter.php';
require_once 'Horde/Help.php';
require_once 'Horde/History.php';

// Start compression, if requested.
Horde::compressOutput();

// Create a share instance.
require_once 'Horde/Share.php';
$GLOBALS['groo_shares'] = &Horde_Share::singleton($registry->getApp());

// Update the preference for which collections to display. If the user
// doesn't have any selected collections for view then fall back to some
// available collection.

//TODO: understand why I needed this workaround as the expression below was returning NULL
// see corresponding code in Mnemo
if (!is_null($GLOBALS['prefs']->getValue('display_collections'))) {
    // this is what Mnemo does
    $GLOBALS['display_collections'] = unserialize($GLOBALS['prefs']->getValue('display_collections'));    
} else {
    $GLOBALS['display_collections'] = array(Auth::getAuth());
}

if (($d_item = Util::getFormData('display_collection')) !== null) {
    if (in_array($d_item, $GLOBALS['display_collections'])) {
        $key = array_search($d_item, $GLOBALS['display_collections']);
        unset($GLOBALS['display_collections'][$key]);
    } else {
        $GLOBALS['display_collections'][] = $d_item;
    }
    $GLOBALS['prefs']->setValue('display_collections', serialize($GLOBALS['display_collections']));
}

// Make sure all task lists exist now, to save on checking later.
$_temp = $GLOBALS['display_collections'];
$_all = Groo::listCollections();
$GLOBALS['display_collections'] = array();
foreach ($_temp as $id) {
    if (isset($_all[$id])) {
        $GLOBALS['display_collections'][] = $id;
    }
}

if (count($GLOBALS['display_collections']) == 0) {
    $lists = Groo::listCollections(true);
    if (!Auth::getAuth()) {
        // All collections for guests.
        $GLOBALS['display_collections'] = array_keys($lists);
    } elseif (!count($lists)) {
        // Create a personal item list
        $GLOBALS['display_collections'] = array(Auth::getAuth());

        // If this share doesn't exist then create it.
        if (!$GLOBALS['groo_shares']->exists(Auth::getAuth())) {
            require_once 'Horde/Identity.php';
            $identity = &Identity::singleton();
            $name = $identity->getValue('fullname');
            if (trim($name) == '') {
                $name = Auth::removeHook(Auth::getAuth());
            }
            $share = &$GLOBALS['groo_shares']->newShare(Auth::getAuth());
            $share->set('name', sprintf(_("%s's Collection"), $name));
            $GLOBALS['groo_shares']->addShare($share);
        }
    }
}

$GLOBALS['prefs']->setValue('display_collections', serialize($GLOBALS['display_collections']));
